/**
 * 跳转函数
 * @param url
 */
function goUrl(url){
    if(-1 === url.indexOf('http')){
        url = getRealPath()+url;
    };

    if(-1 === url.indexOf('?')){
        window.location.href=url+"?v="+(new Date().getTime());
    }else{
        window.location.href=url+"&v="+(new Date().getTime());
    }
}

/**
 * 跳转函数
 * @param router
 * @param paramter
 */
function goRouter(router,paramter){
    if(-1 !== router.indexOf('http://')){
        window.location.href = router;
        return;
    };

    var url = getRealPath() + router;
    if(null !== paramter && '' !== paramter && undefined !== paramter){
        for(var i = 0; i < paramter.length; i++){
            url += '&' + paramter[i].text + '=' + paramter[i].value;
        }
    }
    window.location.href = url + "&v=" + (new Date().getTime());
}

/**
 * 返回上一页强制刷新页面
 */
function init(){
    var isPageHide = false;
    window.addEventListener('pageshow', function () {
        if (isPageHide) {
            window.location.reload();
        }
    });
    window.addEventListener('pagehide', function () {
        isPageHide = true;
    });
}

/**
 * 转化为doubel类型(主要是为了金钱类型的显示)
 * @param num
 * @returns {String}
 */
function getDoubelStr(num){
    num += '';
    num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符  

    if(/^0+/) //清除字符串开头的0  
        num = num.replace(/^0+/, '');
    if(!/\./.test(num)) //为整数字符串在末尾添加.00  
        num += '.00';
    if(/^\./.test(num)) //字符以.开头时,在开头添加0  
        num = '0' + num;
    num += '00';        //在字符串末尾补零  
    num = num.match(/\d+\.\d{2}/)[0];
    return num;
};

/**
 * 获取项目名，域名+端口，获取http
 * @returns {String}
 */
function getRealPath(getWath){
    var localObj = window.location;
    var contextPath = localObj.pathname.split("/")[1];
    var basePath = localObj.protocol + "//" + localObj.host + "/"+ contextPath;

    if(null !== getWath && '' !== getWath && undefined !== getWath){
        if('protocol' === getWath){
            return localObj.protocol;
        }else if('host' === getWath){
            return localObj.host;
        }else if('contextPath' === getWath){
            return localObj.contextPath;
        }else{
            return basePath ;
        }
    }else{
        return basePath ;
    }
}

/**
 * [isMobile 判断平台]
 * @param test: 0:iPhone    1:Android
 */
function ismobile(test){
    var u = navigator.userAgent, app = navigator.appVersion;
    if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
        if(window.location.href.indexOf("?mobile")<0){
            try{
                if(/iPhone|mac|iPod|iPad/i.test(navigator.userAgent)){
                    return '0';
                }else{
                    return '1';
                }
            }catch(e){}
        }
    }else if( u.indexOf('iPad') > -1){
        return '0';
    }else{
        return '1';
    }
};

/**
 * 判断css3动画是否执行完毕
 * @git http://blog.alexmaccaw.com/css-transitions
 * @param duration
 */
function emulateTransitionEnd(duration) {
    var called = false,
        $el = this;

    $(this).one('webkitTransitionEnd', function () {
        called = true;
    });

    var callback = function () {
        if (!called) $($el).trigger('webkitTransitionEnd');
    };

    setTimeout(callback, duration);
};

/**
 * 根据路径显示图片
 * @param src
 */
function show_picture(src){
    if(!src){return;}
    var id = new Date().getTime() + Math.round(Math.random()*10) + Math.round(Math.random()*10);
    $('body').append('<img id='+id+' style="position: absolute;top: 0;right: 0;left: 0;bottom: 0;margin: auto;width: 80%;z-index: 9000;display: none;" src="'+src+'">');

    $('body').append('<div class="weui-mask"></div>');
    $('.weui-mask').addClass('weui-animate-fade-in');
    $('#'+id).show();
    $('#'+id).addClass('weui-animate-fade-in').on('animationend webkitAnimationEnd', function () {
        $('#'+id).off('animationend webkitAnimationEnd');
        $('.weui-mask').on('touchend', function () {
            $('.weui-mask').addClass('weui-animate-fade-out');
            $('#'+id).addClass('weui-animate-fade-out').on('animationend webkitAnimationEnd', function () {
                $('.weui-mask').remove();
                $('#'+id).removeClass('weui-animate-fade-in weui-animate-fade-out');
                $('#'+id).off('animationend webkitAnimationEnd');
                $('#'+id).hide();
            });
        });
        $('#'+id).on('touchend', function () {
            $('.weui-mask').addClass('weui-animate-fade-out');
            $('#'+id).addClass('weui-animate-fade-out').on('animationend webkitAnimationEnd', function () {
                $('.weui-mask').remove();
                $('#'+id).removeClass('weui-animate-fade-in weui-animate-fade-out');
                $('#'+id).off('animationend webkitAnimationEnd');
                $('#'+id).hide();
            });
        });
    });
}

//失去焦点以后调用一下页面滚动
function window_scroll(src){
    for(var i=0;i<src.length;i++) {
        src[i].onblur = function () {
            var currentPosition,timer;
            var speed=1;//页面滚动距离
            timer=setInterval(function(){
                currentPosition=document.body.scrollTop;
                currentPosition-=speed;
                window.scrollTo(0,currentPosition);//页面向上滚动
                currentPosition+=speed; //speed变量
                window.scrollTo(0,currentPosition);//页面向下滚动
                clearInterval(timer);
            },1);
        }
    }
}

/**
 * 解决:active这个高端洋气的CSS伪类不能使用问题
 */
!function (window) {
    window.document.addEventListener('touchstart', function (event) {}, false);

    //处理安卓机的输入法挡住input
    if (/Android/gi.test(navigator.userAgent)) {
        window.addEventListener('resize', function () {
            if (document.activeElement.tagName == 'INPUT' || document.activeElement.tagName == 'TEXTAREA') {
                window.setTimeout(function () {
                    document.activeElement.scrollIntoViewIfNeeded();
                }, 0);
            }
        })
    }

    //处理ios页面内容不下滑
    if (!/Android/gi.test(navigator.userAgent)) {
        window_scroll(document.getElementsByTagName('input'));
        window_scroll(document.getElementsByTagName('textarea'));
    }
}(window);



